package com.dk.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dk.mapper.MdVendorMapper;
import com.dk.pojo.CodeRule;
import com.dk.pojo.MdVendor;
import com.dk.pojo.MdVendorExample;
import com.dk.pojo.User;
import com.dk.result.PageResult;
import com.dk.result.Result;
import com.dk.result.ResultConstant;
import com.dk.service.VendorService;
import com.dk.utils.JwtUtil;
import com.github.pagehelper.PageInfo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author LTZ
 * @create 2023/5/11
 */
@Service
public class VendorServiceImpl implements VendorService {

    @Autowired
    private MdVendorMapper vendorMapper;
    @Autowired
    private JwtUtil jwtUtil;

    @Override
    public PageResult<MdVendor> getVendorInfo(Integer pageNum, Integer pageSize, String vendorCode, String vendorName, String enableFlag) {
        MdVendorExample example = new MdVendorExample();
        if(vendorName != null && vendorName.length() > 0){
            example.createCriteria().andVendorNameLike("%"+vendorName+"%");
        }
        if(vendorCode != null && vendorCode.length() > 0){
            example.createCriteria().andVendorCodeEqualTo(vendorCode);
        }
        if(enableFlag != null && enableFlag.length() > 0){
            example.createCriteria().andEnableFlagEqualTo(enableFlag);
        }
        List<MdVendor> mdVendors = vendorMapper.selectByExample(example);
        PageInfo<MdVendor> pb = new PageInfo<>(mdVendors);
        PageResult<MdVendor> result = new PageResult<>(pb.getPageNum(), pb.getPageSize(), pb.getTotal(),pb.getList());
        return result;

    }

    @Override
    public Result addVendor(MdVendor vendor, String token) {
        try {
            Claims claims = jwtUtil.parseJwt(token);
            String subject = claims.getSubject();
            User user = JSONObject.parseObject(subject, User.class);
            vendor.setCreateBy(user.getUsername());
            vendor.setCreateTime(new Date());
            vendorMapper.insertSelective(vendor);
            return  new Result(ResultConstant.CODE,ResultConstant.MESSAGE);
        } catch (Exception e) {
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
    }

    @Override
    public Result updateVendor(MdVendor vendor, String token) {
        try {
            Claims claims = jwtUtil.parseJwt(token);
            String subject = claims.getSubject();
            User user = JSONObject.parseObject(subject, User.class);
            vendor.setUpdateBy(user.getUsername());
            vendor.setUpdateTime(new Date());
            vendorMapper.updateByPrimaryKeySelective(vendor);
            return  new Result(ResultConstant.CODE,ResultConstant.MESSAGE);
        } catch (Exception e) {
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
    }

    @Override
    public void deleteVendor(Long vendorId) {
        vendorMapper.deleteByPrimaryKey(vendorId);
    }

    @Override
    public MdVendor selectByVendorCode(String vendorCode) {
        MdVendorExample example = new MdVendorExample();
        example.createCriteria().andVendorCodeEqualTo(vendorCode);
        List<MdVendor> mdVendors = vendorMapper.selectByExample(example);
        return mdVendors.size() > 0 ? mdVendors.get(0): null;
    }
}